﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Universum.Infrastructure.Persistence;
using Universum.Infrastructure.Persistence.Queries.Classes;
using System.Security.Principal;
using Universum.Model.Domain.Classes;
using Universum.Infrastructure.Persistence.Queries.MyClasses;

namespace Universum.Model.Services.Classes
{
	public class ClassesServiceImpl : IClassesService
	{
		IRepository _Repository;
		IPrincipal _Principal;
		IFriendlyUrlService _SlugService;
		IPageGroupService _PageGroupService;
		public ClassesServiceImpl(
			IFriendlyUrlService slugService,
			IRepository repository,
			IPrincipal principal,
			IPageGroupService pageGroupService
			
			)
		{
			_SlugService = slugService;
			_Repository = repository;
			_Principal = principal;
			_PageGroupService = pageGroupService;
		}

		#region IClassesService Members

		public IList<Universum.Model.Domain.Classes.Class> GetClassesForUser(string username)
		{
			
			return _Repository.Query(new ClassByUsername(username)).ToList();
		}

		#endregion

		#region IClassesService Members


		public Universum.Model.Domain.Classes.Class CreateNewClass()
		{
			return new Class
			{
				Owner = _Principal.Identity.Name,
				ID = Guid.NewGuid()			
				
			};
		}

		public void SaveOrUpdateClass(Universum.Model.Domain.Classes.Class save)
		{
			save.Slug = _SlugService.CreateSlugFrom(save.Name);
			_Repository.SaveOrUpdate(save);
		}

		public Class GetClassById(Guid id)
		{
			return _Repository.Load<Class>(id);
		}

		
		public void Remove(Class model)
		{
			_PageGroupService.RemoveByClassId(model.ID);
			_Repository.Remove(model);
		}

	
		public IList<Class> GetAllClasses()
		{
			return _Repository.Query<Class>().ToList();
		}

		#endregion

		#region IClassesService Members


		public Class GetClassBySlug(string id)
		{
			return _Repository.Query(new ClassBySlug(id)).FirstOrDefault();
		}

		#endregion
	}
}
